home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-05-13 | 3.0 KB | 106 lines | [TEXT/KAHL] |
- // Bolo code (C) Stuart Cheshire <cheshire@cs.stanford.edu> 1987-1995.
- // All rights reserved. This code is owned by Stuart Cheshire and is donated
- // free of charge for non-commercial use. You may not use this code in any
- // product sold for commercial profit, except shareware priced at $25 or less.
-
- #include <Types.h>
- #include <Files.h>
-
- #include "BrainFrame.h"
- #include "BF_Main.h"
- #include "BF_Utils.h"
- #include "BF_Prefs.h"
-
- #define CURRENT_PREFS_VERSION 1
-
- local PREFS prefs_on_disk;
- export PREFS prefs;
-
- export FSSpec PrefsFile;
- local const u_char defaultname[] = "\pBrainFrame Preferences";
-
- local void FindPrefsFile(Boolean create)
- {
- int i;
- Str255 filename;
- HParamBlockRec fi;
-
- fi.fileParam.ioCompletion = 0L;
- fi.fileParam.ioNamePtr = (StringPtr)&filename;
- fi.fileParam.ioVRefNum = PrefsFolderWD;
- fi.fileParam.ioDirID = 0;
- fi.fileParam.ioFDirIndex = 1;
-
- while (PBHGetFInfo(&fi, false) == noErr)
- {
- if (fi.fileParam.ioFlFndrInfo.fdType == 'PREF' &&
- fi.fileParam.ioFlFndrInfo.fdCreator == CREATOR_CODE)
- {
- PrefsFile.vRefNum = PrefsFolderWD;
- PrefsFile.parID = 0;
- for (i=0; i<=filename[0]; i++) PrefsFile.name[i] = filename[i];
- return;
- }
- fi.fileParam.ioFDirIndex++;
- fi.fileParam.ioDirID = 0;
- }
- if (create)
- {
- PrefsFile.vRefNum = PrefsFolderWD;
- PrefsFile.parID = 0;
- for (i=0; i<=defaultname[0]; i++) PrefsFile.name[i] = defaultname[i];
- Create(PrefsFile.name, PrefsFile.vRefNum, CREATOR_CODE, 'PREF');
- }
- }
-
- local short OpenPrefsFile(short *fRef, Boolean for_writing)
- {
- short permission = fsRdPerm;
- if (for_writing) permission = fsRdWrPerm;
- *fRef = 0;
- if (!PrefsFile.vRefNum) FindPrefsFile(for_writing);
- if (!PrefsFile.vRefNum) return(1);
- if (PrefsFile.parID) return(FSpOpenDF(&PrefsFile, permission, fRef));
- else return(FSOpen(PrefsFile.name, PrefsFile.vRefNum, fRef));
- }
-
- local void set_default_prefs(void)
- {
- prefs_on_disk.prefs_version = 0; // mark the disk copy as garbage
- prefs.prefs_version = CURRENT_PREFS_VERSION; // and create new prefs in memory
- prefs.frametime = 2;
- }
-
- // if prefs != prefs_on_disk then we write back to disk
- // (otherwise don't do it, to avoid spinning up the disk)
- export void save_prefs(void)
- {
- if (prefs.prefs_version != CURRENT_PREFS_VERSION) return;
- if (compare_structs(prefs, prefs_on_disk))
- {
- short fRef;
- if (OpenPrefsFile(&fRef, TRUE) == noErr)
- {
- long len = sizeof(PREFS);
- prefs_on_disk = prefs;
- SetEOF(fRef,0); // truncate the file before writing new data
- FSWrite(fRef, &len, &prefs_on_disk);
- FSClose(fRef);
- }
- }
- }
-
- export void readprefs(void) // Only called ONCE, at startup
- {
- short fRef;
- long len;
- OSErr readcode=0, opencode = OpenPrefsFile(&fRef, FALSE);
- if (opencode == noErr && GetEOF(fRef, &len) == noErr && len == sizeof(PREFS))
- readcode = FSRead(fRef, &len, &prefs_on_disk);
- prefs = prefs_on_disk;
- if (opencode || readcode || prefs.prefs_version != CURRENT_PREFS_VERSION)
- set_default_prefs();
- if (opencode == noErr) FSClose(fRef);
- save_prefs(); // If we created new default prefs this will write them to disk
- }
-